<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>垃圾分类列表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../layui/css/layui.css" media="all">
    <script src="../js/jquery-3.1.0.min.js"></script>
    <script src="../layui/layui.js" charset="utf-8"></script>
    <script src="../js/public.js"></script>
</head>
<body>
<div class="demoTable">
    <!--分类名称：
    <div class="layui-inline">
    <input class="layui-input" name="name" id="name" autocomplete="off">
    </div>-->
    垃圾分类：
    <div class="layui-inline">
        <select name="type" id="type" autocomplete="off" class="layui-input" style="width: 120px;padding-left: 5px">
            <option value=""> -- 请选择 --</option>
            <option value="ordinary">普通垃圾</option>
            <option value="recyclable">可回收垃圾</option>
            <option value="large">大件垃圾</option>
        </select>
    </div>
    <button class="layui-btn" data-type="reload">搜索</button>
    <button type="button" class="layui-btn" id="addtype"><i class="layui-icon"></i></button>
</div>
<table class="layui-hide" id="types" lay-filter="types"></table>

<script>
    layui.use(['table', 'layer'], function () {
        var table = layui.table;
        var $ = layui.jquery, layer = layui.layer; //独立版的layer无需执行这一句
        //方法级渲染
        table.render({
            elem: '#types'
            , url: '/refuse-web/api/refuse/typecondition'
            , cols: [[
                // {checkbox: true, fixed: true}
                {field: 'id', title: 'ID', width: 80, sort: true, fixed: true}
                , {field: 'type', title: '所属分类', templet: '#typeformat'}
                , {field: 'name', title: '类型名称'}
                , {field: 'price', title: '回收价格'}
                , {field: 'unit', title: '价格单位'}
                , {align: 'center', title: '操作', toolbar: '#toolBar'}
            ]]
            , id: 'typeList'
            , page: true
            , lodding: true
        });

        //监听添加按钮
        $(document).on('click', '#addtype', function () {
            layer.open({
                type: 2 //此处以iframe举例
                , title: '新增垃圾分类'
                , area: ['380px', '360px']
                , shade: 0
                , maxmin: true
                , content: 'addtype.html'
                , btn: ['关闭'] //只是为了演示
                , btn: function () {
                    layer.closeAll();
                }
                , zIndex: layer.zIndex //重点1
                , success: function (layero) {
                    layer.setTop(layero); //重点2
                }
            });
        });

        //监听工具条
        table.on('tool(types)', function (obj) {
            var data = obj.data;
            if (obj.event === 'del') {
                layer.confirm('确定删除吗，删除后不可恢复?', function (index) {
                    //向服务端发送删除指令
                    $.ajax({
                        url: '/refuse-web/api/refuse/deltype',
                        type: 'get',
                        datyaType: "json",
                        data: {'id': data.id},//向服务端发送删除的id
                        success: function (res) {
                            if (res.code == 0) {
                                layer.msg("删除成功", {icon: 1, time: 1000}, function () {
                                    obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
                                    layer.close(index);
                                });
                            } else {
                                layer.msg("删除失败", {icon: 5, time: 1000}, function () {
                                    obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
                                    layer.close(index);
                                });
                            }
                            location.reload();
                            layer.close(index);
                        },
                        error: function (msg) {
                            layer.msg("服务器请求失败！", {icon: 5})
                        }
                    })
                });
            } else if (obj.event === 'edit') {
                layer.open({
                    type: 2 //此处以iframe举例
                    , title: '修改分类信息'
                    , area: ['380px', '420px']
                    , shade: 0
                    , maxmin: true
                    , content: 'updatetype.html?id=' + data.id
                    , btn: ['关闭'] //只是为了演示
                    , btn: function () {
                        layer.closeAll();
                    }
                    , zIndex: layer.zIndex //重点1
                    , success: function (layero) {
                        layer.setTop(layero); //重点2
                    }
                });
            }
        });

        var $ = layui.$, active = {
            reload: function () {
                var type = $('#type');
                //执行重载
                table.reload('typeList', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    , where: {
                        type: type.val()
                    }
                }, 'data');
            },

            setTop: function () {
                var that = this;
                //多窗口模式，层叠置顶
                layer.open({
                    type: 2 //此处以iframe举例
                    , title: '新增垃圾种类'
                    , area: ['250px', '200px']
                    , shade: 0
                    , maxmin: true
                    , content: 'addtype.html'
                    , zIndex: layer.zIndex //重点1
                    , success: function (layero) {
                        layer.setTop(layero); //重点2
                    }
                });
            }
        };

        $('.demoTable .layui-btn').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });
</script>
<script type="text/html" id="toolBar">
    <!--<a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">查看</a>-->
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <!--<button data-method="setTop" data-type="auto" class="layui-btn layui-btn-xs">编辑</button>-->
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<script type="text/html" id="typeformat">
    {{# if(d.type == 'ordinary'){ }}
    普通垃圾
    {{# }else if(d.type == 'recyclable'){ }}
    可回收垃圾
    {{# }else if(d.type == 'large'){ }}
    大件垃圾
    {{# }else{ }}
    啥子东西哟
    {{# } }}
</script>
</body>
</html>